package com.hb.game.pk.adpter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.utils.SpannableStringUtils;
import com.hb.game.pk.utils.StringUtils;

import java.util.List;
import java.util.Map;

public class ListViewAdapter extends BaseAdapter {

    private ListView mListView;
    private Activity act;
    private List<Map> data;
    private int type;

    public ListViewAdapter(Activity act, ListView rvList) {
        this.act = act;
        mListView = rvList;
    }

    public void setNewData(List<Map> data) {
        this.data = data;
        notifyDataSetChanged();
        mListView.setSelection(getCount() - 1);
    }

    public void addMore(Map map) {
        this.data.add(map);
        notifyDataSetChanged();
//        mListView.setSelection(mListView.getCount() - 1);
    }

    public void setStatus(int position) {
        Map map = data.get(position);
        map.put("cliced", true);
        data.set(position, map);
        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
    }

    public List<Map> getData() {
        return this.data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(act, R.layout.item_chat_list_2, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Map item = data.get(position);
        holder.rlLeft.setVisibility(View.GONE);
        holder.rlRight.setVisibility(View.GONE);
        holder.tvLq.setVisibility(View.GONE);
        holder.tvZj.setVisibility(View.GONE);
        if ((item.containsKey("m") && Float.parseFloat(item.get("m").toString()) == 1.0f) || !item.containsKey("m")) {
            String username = "";
            String money = "";
            int res = 0;
            if (type != 1) {
                if (!item.containsKey("cliced")) {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_normal);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_normal);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_blue_left);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_blue_right);
                } else {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_clicked);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_clicked);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_blue_left_clicked);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_blue_right_clicked);
                }
            } else {
                if (!item.containsKey("cliced")) {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_normal);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_normal);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_chat_left);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_chat_right);
                } else {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_chat_left_clicked);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_chat_right_cliced);
                }
            }
            if (!item.containsKey("m")) {
                String trim = StringUtils.getString(item.get("avatar").toString());
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                username = item.get("username").toString();
                money = Float.parseFloat(item.get("money").toString()) / 100 + "";
                if (APP.userInfo.getUser_id().equals(item.get("user_id").toString())) {
                    holder.rlRight.setVisibility(View.VISIBLE);
                    holder.ivHeadRight.setBackgroundResource(res);
                    holder.tvNameRight.setText(username);
                    if (type == 1) {
                        holder.tvMoneyRight.setText(money + "-" + item.get("bom_num").toString());
                    } else {
                        if ("0".equals(item.get("user_id").toString()))
                            holder.tvMoneyRight.setText("系统发包");
                        else
                            holder.tvMoneyRight.setText(money);
                    }
                } else {
                    holder.rlLeft.setVisibility(View.VISIBLE);
                    holder.ivHeadLeft.setBackgroundResource(res);
                    holder.tvNameLeft.setText(username);
                    if (type == 1) {
                        holder.tvMoneyLeft.setText(money + "-" + item.get("bom_num").toString());
                    } else {
                        if ("0".equals(item.get("user_id").toString()))
                            holder.tvMoneyLeft.setText("系统发包");
                        else
                            holder.tvMoneyLeft.setText(money);
                    }
                }
            } else {
                Map<String, Object> data = (Map<String, Object>) item.get("data");
                String trim = StringUtils.getString(data.get("avatar").toString());
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                username = data.get("username").toString();
                money = Float.parseFloat(data.get("money").toString()) / 100 + "";
                if (APP.userInfo.getUser_id().equals(data.get("user_id").toString())) {
                    holder.rlRight.setVisibility(View.VISIBLE);
                    holder.ivHeadRight.setBackgroundResource(res);
                    holder.tvNameRight.setText(username);
                    if (type == 1) {
                        holder.tvMoneyRight.setText(money + "-" + data.get("bom_num").toString());
                    } else {
                        if ("0".equals(data.get("user_id").toString()))
                            holder.tvMoneyRight.setText("系统发包");
                        else
                            holder.tvMoneyRight.setText(money);
                    }
                } else {
                    holder.rlLeft.setVisibility(View.VISIBLE);
                    holder.ivHeadLeft.setBackgroundResource(res);
                    holder.tvNameLeft.setText(username);
                    if (type == 1) {
                        holder.tvMoneyLeft.setText(money + "-" + data.get("bom_num").toString());
                    } else {
                        if (0==Double.parseDouble(data.get("user_id").toString()))
                            holder.tvMoneyLeft.setText("系统发包");
                        else
                            holder.tvMoneyLeft.setText(money);
                    }
                }
            }
        } else if (item.containsKey("m") && Float.parseFloat(item.get("m").toString()) == 2.0f) {
            holder.tvZj.setVisibility(View.VISIBLE);
            Map<String, Object> data = (Map<String, Object>) item.get("data");
            String name = data.get("username").toString();
            float money = Float.parseFloat(data.get("aword").toString());
            holder.tvZj.setText(SpannableStringUtils.getBuilder(name)
                    .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                    .append("中奖").setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                    .append((money / 100) + "元").setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                    .create());
        } else {
            holder.tvLq.setVisibility(View.VISIBLE);
            Map<String, Object> data = (Map<String, Object>) item.get("data");
            String name = data.get("username").toString();
            holder.tvLq.setText(SpannableStringUtils.getBuilder(name)
                    .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                    .append("领取了您的红包").setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                    .create());
        }
        return convertView;
    }

    public class ViewHolder {
        private LinearLayout llRedBoxLeft;
        private LinearLayout llRedBoxRight;
        private ImageView ivRightRight;
        private ImageView ivLeftLeft;
        private TextView tvLq;
        private TextView tvZj;
        private TextView tvMoneyLeft;
        private TextView tvNameLeft;
        private ImageView ivHeadLeft;
        private RelativeLayout rlLeft;
        private TextView tvMoneyRight;
        private TextView tvNameRight;
        private ImageView ivHeadRight;
        private RelativeLayout rlRight;

        public ViewHolder(View view) {
            rlLeft = (RelativeLayout) view.findViewById(R.id.rl_left);
            ivHeadLeft = (ImageView) view.findViewById(R.id.iv_head_left);
            tvNameLeft = (TextView) view.findViewById(R.id.tv_name_left);
            tvMoneyLeft = (TextView) view.findViewById(R.id.tv_money_left);
            rlRight = (RelativeLayout) view.findViewById(R.id.rl_right);
            ivHeadRight = (ImageView) view.findViewById(R.id.iv_head_right);
            tvNameRight = (TextView) view.findViewById(R.id.tv_name_right);
            tvMoneyRight = (TextView) view.findViewById(R.id.tv_money_right);
            tvZj = (TextView) view.findViewById(R.id.tv_zj);
            tvLq = (TextView) view.findViewById(R.id.tv_lq);
            ivLeftLeft = (ImageView) view.findViewById(R.id.iv_left_left);
            ivRightRight = (ImageView) view.findViewById(R.id.iv_right_right);
            llRedBoxRight = (LinearLayout) view.findViewById(R.id.ll_red_box_right);
            llRedBoxLeft = (LinearLayout) view.findViewById(R.id.ll_red_box_left);
        }
    }
}
